package com.icss.ah.expressBird.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * 快递鸟回调service
 * @author csadchen
 * @date 2017-06-15
 */
@Service
public class ExpressBirdService {
	//错误日志
	private final Logger errlog = Logger.getLogger("E");
	//info日志
	private final Logger infolog = Logger.getLogger("I");
	
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	/**
	 * 快递单信息保存
	 * @param json
	 * @return
	 */
	@Transactional
	public boolean saveExpressLogistic(JSONObject json){
		boolean result = false;
		try{
			//json转map
			HashMap info = (HashMap) JSONObject.toBean(json, HashMap.class);
			//快递单号
			String LogisticCode = json.getString("LogisticCode");
			//记录日志
			infolog.info("ExpressBirdService|saveExpressLogistic|LogisticCode:"+LogisticCode);
			//获取快递单物流转运信息
			List traces = json.getJSONArray("Traces");
			//记录日志
			infolog.info("ExpressBirdService|saveExpressLogistic|LogisticCode:"+LogisticCode+"|traces:"+traces);
			//map中去除物流转运信息
			info.remove("Traces");
			//获取当前快递单是否已有数据
			Integer count = sqlSessionTemplate.selectOne("ExpressBirdBean.queryExpressLogisticCount", info);
			if(count>0){
				//已有数据进行更新操作
				sqlSessionTemplate.update("ExpressBirdBean.updateExpressLogistic", info);
			} else {
				//无数据进行插入操作
				sqlSessionTemplate.insert("ExpressBirdBean.insertExpressLogistic", info);
			}
			//物流转运信息列表
			List list = new ArrayList();
			//循环获取物流转运信息
			for(Iterator<JSONObject> d = traces.iterator();d.hasNext();){
				//获取物流转运信息
				JSONObject tracesInfo = d.next();
				//物流转运信息map
				HashMap tracesMap = new HashMap();
				//转运信息
				String AcceptStation = tracesInfo.getString("AcceptStation");
				//转运时间
				String AcceptTime = tracesInfo.getString("AcceptTime");
				//将物流转运信息放到map中
				tracesMap.put("LogisticCode", LogisticCode);
				tracesMap.put("AcceptStation", AcceptStation);
				tracesMap.put("AcceptTime", AcceptTime);
				list.add(tracesMap);
			}
			//记录日志
			infolog.info("ExpressBirdService|saveExpressLogistic|LogisticCode:"+LogisticCode+"|list:"+list);
			if(list.size()>0){
				//删除现有转运信息
				sqlSessionTemplate.delete("ExpressBirdBean.deleteExpressLogisticDetail", LogisticCode);
				//插入物流转运信息
				sqlSessionTemplate.insert("ExpressBirdBean.insertExpressLogisticDetail", list);
				//更新订单物流信息表
				sqlSessionTemplate.insert("ExpressBirdBean.insertOrderLogistic", LogisticCode);
			}
			//返回结果
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			//记录日志
			errlog.error("ExpressBirdService|saveExpressLogistic|异常：",e);
		}
		return result;
	}
	/**
	 * 获取快递单是否有对应数据
	 * @param LogisticCode
	 * @return
	 */
	public int queryExpressLogisticCount(String LogisticCode){
		Integer count = 0;
		try{
			count = sqlSessionTemplate.selectOne("ExpressBirdBean.queryExpressLogisticCount", LogisticCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}
}
